﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

//#define VERSION_ID_1 // Anglicka verzia 64 bit
#define VERSION_ID_2 // Anglicka verzia 32 bit
//#define VERSION_ID_3 // Slovenska verzia 64 bit
//#define VERSION_ID_4 // Slovenska verzia 32 bit

#define LANGUAGE_SK

using System;

namespace core
{
    /// <summary>Trieda totalem</summary>
    public class totalem
    {
#if VERSION_ID_1
        public const string CURRENT_VERSION = "2012.05.30.1 - 64 bit";
        public const string CURRENT_VERSION_HASH_TAG = "2012_05_30_1_64_en";
#endif
#if VERSION_ID_2
        public const string CURRENT_VERSION = "2012.05.30.1 - 32 bit";
        public const string CURRENT_VERSION_HASH_TAG = "2012_05_30_1_32_en";
#endif
#if VERSION_ID_3
        public const string CURRENT_VERSION = "2012.05.30.1 - 64 bit";
        public const string CURRENT_VERSION_HASH_TAG = "2012_05_30_1_64_sk";
#endif
#if VERSION_ID_4
        public const string CURRENT_VERSION = "2012.05.30.1 - 32 bit";
        public const string CURRENT_VERSION_HASH_TAG = "2012_05_30_1_32_sk";
#endif

        protected static System.Threading.Mutex AppMutex; // Aplikacny mutex

        /// <summary>Inicializacia aplikacie</summary>
        /// <returns>0 alebo kod chyby</returns>
        public static int InitApplication()
        {
            System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);
            System.Windows.Forms.Application.EnableVisualStyles(); // Natiahnem manifest
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
            
            AppMutex = null;

            new IApp(); // Inicializujem IApp objekt
            try
            {
                AppMutex = System.Threading.Mutex.OpenExisting("totalem_mutex"); // Otvorim existujuci mutex
                if (IApp.Settings.App_OnlyOneInstance) // Len jedna instancia?
                {
                    return 1;
                }
            }
            catch // Chyba?
            {
                AppMutex = new System.Threading.Mutex(false, "totalem_mutex"); // Vytvorim novy
            }
            new IAppForm(); // Vytvorim hlavne okno

            return 0;
        }
        /// <summary>Spustenie aplikacie</summary>
        /// <returns>0 alebo kod chyby</returns>
        public static int RunApplication()
        {
            IApp.AppForm.Show(); // Zobrazim hlavne okno
            System.Windows.Forms.Application.Run(); // Spustim aplikaciu
            IApp.AppForm = (IAppForm)ICore.DisposeObject(IApp.AppForm); // Zrusim objekt

            return 0;
        }
        /// <summary>Uzatvorenie aplikacie</summary>
        public static void CloseApplication()
        {
            IApp.AppForm = (IAppForm)ICore.DisposeObject(IApp.AppForm); // Zrusim objekt
            IApp.App = (IApp)ICore.DisposeObject(IApp.App); // Zrusim objekt
            AppMutex = (System.Threading.Mutex)ICore.DisposeObject(AppMutex); // Zrusim mutex            
        }
    }
}
